package com.hongyi.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongyi.web.dto.SysUserDto;
import com.hongyi.web.mapper.*;
import com.hongyi.web.pojo.SysUser;
import com.hongyi.web.pojo.SysUserPost;
import com.hongyi.web.pojo.SysUserRole;
import com.hongyi.web.query.SysUserQueryDto;
import com.hongyi.web.service.ISysUserService;
import com.hongyi.web.utils.PageResult;
import com.hongyi.web.utils.StringUtils;
import com.hongyi.web.vo.SysUserVo;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysUserServiceImpl implements ISysUserService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;


    @Override
    public PageResult<SysUserVo> selectPage(SysUserQueryDto sysUserQueryDto, Integer pageNum, Integer pageSize) {
             // 进行参数设置
        PageHelper.startPage(pageNum, pageSize);

        List<SysUser> sysUsersList = userMapper.selectUserList(sysUserQueryDto);
        List<SysUserVo> sysUserVosList = new ArrayList<>();
        if (sysUsersList.size() > 0 && !sysUsersList.isEmpty()) {
            for (SysUser sysUser : sysUsersList) {
                SysUserVo sysUserVo = new SysUserVo();
                BeanUtils.copyProperties(sysUser, sysUserVo);
                sysUserVosList.add(sysUserVo);
            }
        }
        // 将结果集进行封装
        PageInfo<SysUserVo> pageInfo = new PageInfo<>(sysUserVosList);
//        return PageInfo.of();
        return  new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    @Transactional
    public void deleteUserByIds(Long[] userIds) {
        // 1.删除 roles表中的数据
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(userIds);
        // 2.删除post表中的数据

        // 删除用户与岗位关联
        userPostMapper.deleteUserPost(userIds);

        // 删除本身数据
        userMapper.deleteUserByIds(userIds);

    }


    /**
     * 通过用户ID查询用户
     *
     * @param userId 用户ID
     * @return 用户对象信息
     */
    @Override
    public SysUser selectUserById(Long userId)
    {
        return userMapper.selectUserById(userId);
    }

    /**
     * 新增保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUser(SysUserDto user)
    {

        SysUser sysUser = new SysUser();
        // 进行复制
        BeanUtils.copyProperties(user, sysUser);
        // 新增用户信息
        int rows = userMapper.insertUser(sysUser);
        // 获取新增用户ID
        Long userId = sysUser.getUserId();
        user.setUserId(userId);
        // 新增用户岗位关联
        insertUserPost(user);
        // 新增用户与角色管理
        insertUserRole(user);
        return rows;
    }


    /**
     * 修改保存用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUser(SysUserDto user)
    {
        Long userId = user.getUserId();
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(user);
        // 删除用户与岗位关联
        userPostMapper.deleteUserPostByUserId(userId);
        // 新增用户与岗位管理
        insertUserPost(user);
        // 更新用户信息
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(user, sysUser);
        return userMapper.updateUser(sysUser);
    }


    /**
     * 修改用户状态
     *
     * @param status 用户状态
     * @return 结果
     */
    @Override
    public int updateUserStatus(String status,Long userId)
    {
//        SysUser user = new SysUser();
//        user.setStatus(status);
        return userMapper.updateUserStatus(status,userId);
    }




    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    public void insertUserPost(SysUserDto user)
    {
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotEmpty(posts))
        {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>(posts.length);
            for (Long postId : posts)
            {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            userPostMapper.batchUserPost(list);
        }
    }

    /**
     * 新增用户角色信息
     *
     */
    public void insertUserRole(SysUserDto  user)
    {
        Long[] roleIds = user.getRoleIds();
        if (StringUtils.isNotEmpty(roleIds))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
            for (Long roleId : roleIds)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            userRoleMapper.batchUserRole(list);
        }
    }

    /**
     * 通过用户ID删除用户
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserById(Long userId)
    {
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 删除用户与岗位表
        userPostMapper.deleteUserPostByUserId(userId);
        return userMapper.deleteUserById(userId);
    }
}
